﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace DDDB
{
    public static class KeyTool
    {
        [ThreadStatic]
        static SHA256? sha256;
        public static byte[] GetHashKey(byte[] src)
        {
            if (sha256 == null)
                sha256 = SHA256.Create();

            if (src.Length == 0)
                throw new Exception("error data");
            return sha256.ComputeHash(src);
        }
        public static byte[] GetHashKey(string src)
        {
            byte[] data = System.Text.Encoding.UTF8.GetBytes(src);
            return GetHashKey(data);
        }

        public static string HashKey2String(byte[] key)
        {
            string str = "";
            for (var i = 0; i < 32; i++)
            {
                str += key[i].ToString("X02");
            }
            return str;
        }
    }

}
